<template>
	<view>
		<scroll-view scroll-x class="bg-white nav">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :class="active==0?'text-green cur':''" @tap="tabSelect(0)">
					活动列表
				</view>
				<!-- <view class="cu-item flex-sub" :class="active==1?'text-green cur':''" @tap="tabSelect(1)">
					已结束
				</view> -->
				<view class="cu-item flex-sub" :class="active==2?'text-green cur':''" @tap="tabSelect(2)">
					我的报名
				</view>
			</view>
		</scroll-view>
		<!---->
		<view v-if="active == 0" class="ppfl_c">
			<view v-if="noData == false">
				<view v-for="(item,index) in waitRepair" :key="index" class="bg-white margin-top margin-right-xs radius margin-left-xs padding">
					<view class="flex padding-bottom-xs solid-bottom justify-between">
						<view>{{item.actId}}</view>
					</view>
					<view class="flex margin-top justify-between">
						<view class="text-gray">活动名称</view>
						<view class="text-gray">{{item.actName}}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">活动时间</view>
						<view class="text-gray">{{item.actTime }}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">联系人</view>
						<view class="text-gray">{{item.contactName}}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">联系电话</view>
						<view class="text-gray">{{item.contactLink }}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">内容</view>
						<view class="text-gray">{{item.context}}</view>
					</view>
					<view class="solid-top flex justify-end margin-top padding-top-sm ">
						<button class="cu-btn sm line-gray" @click="activityDetail(item)">详情</button>
						<button class="cu-btn sm bg-green margin-left" @click="doSignup(item)">报名</button>
					</view>
				</view>
			</view>
			<view v-else>
				<no-data-page></no-data-page>
			</view>
		</view>

		<view v-if="active == 1" class="ppfl_c">
			<view v-if="noData == false">
				<view v-for="(item,index) in doingRepair" :key="index" class="bg-white margin-top margin-right-xs radius margin-left-xs padding">
					<view class="flex padding-bottom-xs solid-bottom justify-between">
						<view>{{item.actId}}</view>
					</view>
					<view class="flex margin-top justify-between">
						<view class="text-gray">活动名称</view>
						<view class="text-gray">{{item.actName}}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">活动时间</view>
						<view class="text-gray">{{item.actTime }}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">联系人</view>
						<view class="text-gray">{{item.contactName}}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">联系电话</view>
						<view class="text-gray">{{item.contactLink }}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">内容</view>
						<view class="text-gray">{{item.context}}</view>
					</view>
					<view class="solid-top flex justify-end margin-top padding-top-sm ">
						<button class="cu-btn sm line-gray" @click="activityDetail(item)">详情</button>
					</view>
				</view>
			</view>
			<view v-else>
				<no-data-page></no-data-page>
			</view>
		</view>

		<view v-if="active == 2" class="ppfl_c">
			<view v-if="noData == false">
				<view v-for="(item,index) in repaired" :key="index" class="bg-white margin-top margin-right-xs radius margin-left-xs padding">
					<view class="flex padding-bottom-xs solid-bottom justify-between">
						<view>{{item.actPerId}}</view>
					</view>
					<view class="flex margin-top justify-between">
						<view class="text-gray">活动名称</view>
						<view class="text-gray">{{item.actName}}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">报名时间</view>
						<view class="text-gray">{{item.createTime }}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">报名人</view>
						<view class="text-gray">{{item.personName}}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">联系电话</view>
						<view class="text-gray">{{item.personLink }}</view>
					</view>
					<view class="flex margin-top-xs justify-between">
						<view class="text-gray">备注</view>
						<view class="text-gray">{{item.remark}}</view>
					</view>
					<view class="solid-top flex justify-end margin-top padding-top-sm ">
						<button class="cu-btn sm line-gray" @click="activityDetail(item)">详情</button>
						<button class="cu-btn bg-red sm margin-left" @click="deleteRepair(item)">删除报名</button>
					</view>
				</view>
			</view>
			<view v-else>
				<no-data-page></no-data-page>
			</view>
		</view>

		<view class="cu-modal" :class="deleteSignup==true?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">温馨提示</view>
					<view class="action" @tap="_cancleDeleteRepair()">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl">
					您确认删除当前报名
				</view>
				<view class="cu-bar bg-white justify-end">
					<view class="action margin-0 flex-sub  solid-left" @tap="_cancleDeleteRepair()">取消</view>
					<view class="action margin-0 flex-sub text-green solid-left" @tap="_doDeleteRepair()">删除</view>
				</view>
			</view>
		</view>
		
		<view class="cu-modal" :class="signupModal==true?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">报名信息</view>
					<view class="action" @tap="_cancleSignUp()">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="cu-form-group">
					<view class="title">姓名</view>
					<input v-model="personName" placeholder="请输入姓名" class="text-right"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">电话</view>
					<input v-model="personLink" placeholder="请输入电话" class="text-right"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">年龄</view>
					<input v-model="personAge" placeholder="请输入年龄" type="number" class="text-right"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">住址</view>
					<input v-model="personAddress" placeholder="请输入住址" class="text-right"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">备注</view>
					<textarea v-model="remark" maxlength="50" placeholder="请输入备注" ></textarea>
				</view>
				<view class="cu-bar bg-white justify-end">
					<view class="action margin-0 flex-sub  solid-left" @tap="_cancleSignUp()">取消</view>
					<view class="action margin-0 flex-sub text-green solid-left" @tap="_doSignup()">确认</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	
	import noDataPage from '@/components/no-data-page/no-data-page.vue'
	import {
		queryGovActivityType,queryGovActivity,queryGovActivityPerson
		,deleteGovActivityPerson,saveGovActivityPerson
	} from '../../api/activity/activityApi.js'
	import {
		getAreaId
	} from '../../api/area/areaApi.js'
	export default {
		data() {
			return {
				personName:'',
				personLink:'',
				personAge:0,
				personAddress:'',
				remark:'',
				active: 0,
				userId: '',
				roomId: '',
				caId: '',
				waitRepair: [],
				doingRepair: [],
				repaired: [],
				deleteSignup: false,
				signupModal: false,
				curRepair: {},
				noData: false,
				preStaffId: '',
				preStaffName: '',
				context: '',
			}
		},
		components: {
			noDataPage
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			let that = this;
			this.vc.onLoad(options);
			that.userId = "-1";
			that.caId = getAreaId();
			that._loadActivities(that.active);
		},
		
		onShow: function(){
			if(this.userId && this.caId){
				this._loadActivities(this.active);
			}
		},
		methods: {
			_loadActivities: function(_active) {
				let that = this;
				let _paramIn = {
					"caId": that.caId,
					"page": 1,
					"row": 10
				};
				let _states = '';
				if (_active == 0) {
					_states = '1000';
					queryGovActivity(_paramIn).then(_data =>{
						let _json = _data;
						if (_json.code == 0 && _json.data.length > 0) {
							let _ownerRepairs = _json.data;
							if (_ownerRepairs.length < 1) {
								that.noData = true;
							} else {
								that.noData = false;
							}
							that.waitRepair = _ownerRepairs;
							return;
						}else{
							uni.showToast({
								title: "查询失败",
								icon: 'none',
								duration: 2000
							})
						}
					})
				} else if (_active == 1) {
					_states = '1100,1300';
				} else {
					queryGovActivityPerson(_paramIn).then(_data =>{
						let _json = _data;
						if (_json.code == 0) {
							let _ownerRepairs = _json.data;
							if (_ownerRepairs.length < 1) {
								that.noData = true;
							} else {
								that.noData = false;
							}
							that.repaired = _ownerRepairs;
							return;
						}else{
							uni.showToast({
								title: "查询失败",
								icon: 'none',
								duration: 2000
							})
						}
					})
					_states = '1200,1700,1800,1900,2000'
				}
				
			},
			onChange: function(e) {
				this._loadActivities(e.detail.index);
			},
			tabSelect: function(_active) {
				this.active = _active;
				this._loadActivities(_active);
			},
			_cancleSignUp: function() {
				this.signupModal = false;
			},
			activityDetail: function(_item) {
				this.vc.navigateTo({
					url: '/pages/activityDetail/activityDetail?actId=' + _item.actId
				});
			},
			deleteRepair: function(_repair) {
				this.curRepair = _repair;
				this.deleteSignup = true;
			},
			_cancleDeleteRepair: function() {
				this.deleteSignup = false;
			},
			doSignup: function(_item) {
				this.curRepair = _item;
				this.signupModal = true;
			},
			_doDeleteRepair: function() {
				let that = this;
				deleteGovActivityPerson(this.curRepair)
					.then(_data => {
						uni.showToast({
							icon: 'none',
							title: '删除成功'
						});
						that._cancleDeleteRepair();
						that._loadActivities(2);
					}, err => {
						uni.showToast({
							icon: 'none',
							title: err
						})
					})
			},
			_appraiseRepair: function(_item) {
				
			},
			_doSignup: function() {
				let that = this;
				if (that.personName == '') {
					uni.showToast({
						title: '姓名不能为空',
						icon: 'none'
					});
					return;
				}
				if (that.personLink == '') {
					uni.showToast({
						title: '电话不能为空',
						icon: 'none'
					});
					return;
				}
				if (that.personAge == 0) {
					uni.showToast({
						title: '年龄不能为空',
						icon: 'none'
					});
					return;
				}
				if (that.personAddress == '') {
					uni.showToast({
						title: '住址不能为空',
						icon: 'none'
					});
					return;
				}
				let _data = {
					caId: getAreaId(),
					actId: that.curRepair.actId,
					personName: that.personName,
					personLink: that.personLink,
					personAge: that.personAge,
					personAddress: that.personAddress,
					remark: that.remark
				};
				saveGovActivityPerson(_data)
					.then(_data => {
						uni.showToast({
							icon: 'none',
							title: '报名成功'
						});
						that._cancleSignUp();
					}, err => {
						uni.showToast({
							icon: 'none',
							title: err
						})
					})
			}
		}
	}
</script>

<style>
	.solid-bottom::after {
		border-bottom: 2upx solid rgba(0, 0, 0, 0.1);
	}
</style>
